<?php
/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class LoginForm extends CFormModel
{
	public $email;
	public $password;
	public $password_repeat;
	public $rememberMe;

	private $_identity;

	/**
	 * Declares the validation rules.
	 * The rules state that username and password are required,
	 * and password needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// email and password are required
			array('email', 'email', 'allowEmpty'=>false, 'checkMX'=>false, 'message'=>BUtils::T('email_address_error_msg')),
			array('email, password', 'required', 'message'=>BUtils::T('cannot_be_blank_msg')),
			array('password_repeat', 'required', 'on'=>'register', 'message'=>BUtils::T('cannot_be_blank_msg')),
			array('password_repeat', 'compare', 'compareAttribute'=>'password', 'on'=>'register'),
			// rememberMe needs to be a boolean
			array('rememberMe', 'boolean', 'on'=>'login'),
			// username or password needs to be authenticated
			array('email', 'authenticate', 'on'=>'register'),
			array('password', 'authenticate', 'on'=>'login'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			'email' => BUtils::T('email_address_lbl'),
			'password' => BUtils::T('password_lbl'),
			'password_repeat' => BUtils::T('password_repeat_lbl'),
			'rememberMe' => BUtils::T('remember_next_time_lbl'),
		);
	}

	/**
	 * Authenticates the password.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute, $params) {
		if (!$this->hasErrors()) {
			if ($params['on'] === 'register') {
				//validate if email exists
				$connection = Yii::app()->db;
				$command = $connection->createCommand('SELECT id FROM tbl_merchant_account WHERE email=:email');
				$row = $command->queryRow(true, array('email' => $this->email));
				$connection->active = false;
				if (empty($row)) {
					
				} else {
					$this->addError('email', BUtils::T('duplicate_email_error_msg'));
				}
			} else {
				$this->_identity = new UserIdentity($this->email, $this->password);
				$this->_identity->authenticate();
				switch($this->_identity->errorCode) {
					case UserIdentity::ERROR_NONE:
						//user login done!
						Yii::app()->user->login($this->_identity);
						break;
					case UserIdentity::ERROR_LOCKED:
						$this->addError('password', BUtils::T('biz_account_locked_msg'));
						break;
					case UserIdentity::ERROR_EXPIRED:
						$this->addError('password', BUtils::T('biz_account_expired_msg'));
						break;
					default:
						$this->addError('password', BUtils::T('login_error_msg'));
						break;
				}
			}
		}
	}

	/**
	 * Logs in the user using the given username and password in the model.
	 * @return boolean whether login is successful
	 */
	public function login()
	{
		if ($this->_identity === null) {
			$this->_identity = new UserIdentity($this->email, $this->password);
			$this->_identity->authenticate();
		}
		if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
			$duration = $this->rememberMe ? 3600*24*30 : 0; // 30 days
			Yii::app()->user->login($this->_identity, $duration);
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Signs up in the user using the given username and password in the model.
	 * @return boolean whether sign-up is successful
	 */
	public function signup() {
		if ($this->_identity === null) {
			$this->_identity = new UserIdentity($this->email, $this->password);
			$this->_identity->createAccount();
		}
		if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
			return true;
		} else {
			$this->addError('username', 'Error when create account! Please contact Yamii support email: admin@yamii.fi.');
			return false;
		}
	}
	
	/**
	 * Reset account infor in this model.
	 * @param string $new_password
	 * @param string $new_email
	 * @return bool
	 */
	public function resetAccount($new_password, $new_email = null) {
		if ($this->_identity === null) {
			$this->_identity = new UserIdentity($this->email, $this->password);
		}
		
		//reset password and email
		return $this->_identity->resetAccount($new_password, $new_email);
	}
	
	/**
	 * Renew a password to th
	 * @param string $new_password
	 * @return bool
	 */
	public function resetPassword($new_password = null) {
		if (empty($new_password)) {
			$password = rand(100000, 999999);
		} else $password = $new_password;
		$this->_identity = new UserIdentity($this->email, $password);
		return $this->_identity->resetPassword();
	}
}
